package org.jetlinks.test.command;

import org.jetlinks.test.event.MonoDeferEvent;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author changfeng.chen@thingsx.com
 * @date 2024/7/8 18:43
 **/
@Component
public class StartupRunner implements CommandLineRunner{
    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void run(String... args) throws Exception {
        MonoDeferEvent monoDeferEvent = new MonoDeferEvent();
        monoDeferEvent.setId("startup");
        // 监听器里面延迟了10秒，也是监听事件跑完，才执行后面
        Mono<Object> then = monoDeferEvent.publish(applicationEventPublisher).then(
            Mono.defer(() -> {
                System.out.println("hello world");
                return Mono.empty();
            })
        );

        then.subscribe();

    }
}
